//
//  AppDelegate.h
//  QQ侧滑菜单Demo
//
//  Created by MCL on 16/7/13.
//  Copyright © 2016年 CHLMA. All rights reserved.
//

#import "LeftMenuTableView.h"
#import "MAMacro.h"
@interface LeftMenuTableView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataSourceArr;
@property (nonatomic, strong) NSArray *imageSourceArr;
@end

@implementation LeftMenuTableView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self =[super initWithFrame:frame];
    if (self) {
        
        self.delegate=self;
        self.dataSource=self;
        self.rowHeight = 40;
        self.separatorColor = [UIColor clearColor];
        self.separatorStyle= UITableViewCellSeparatorStyleNone;
        self.backgroundColor = GreenColor;
        
        UIView *HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 90)];
        HeaderView.backgroundColor = [UIColor whiteColor];
        self.tableHeaderView = HeaderView;
        
        self.logoImage = [[SDNetImageView alloc]initWithFrame:CGRectMake(15, 15, 60, 60)];
        [self.logoImage setShearRound:30 masksToBounds:YES borderWidth:0 borderColor:nil];
        self.logoImage.backgroundColor = GreenColor;
        [HeaderView addSubview:self.logoImage];
        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.logoImage.XWidth+10, self.logoImage.YHighly-20, self.width-self.logoImage.XWidth-20, 20)];
        self.nameLabel.font = fontSizeNormal;
        self.nameLabel.text = @"123";
        self.nameLabel.textColor = MAINBLUEGrayDark;
        [HeaderView addSubview:self.nameLabel];
        
        UITapGestureRecognizer *HeaderViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(HeaderViewTapClick)];
        [HeaderView addGestureRecognizer:HeaderViewTap];
        
        self.dataSourceArr = @[@"数据统计",@"按摩程序",@"热门活动",@"官方商城",@"诺嘉官网",@"售后服务",@"我的设备",@"清理缓存",@"意见反馈"];
    }
    return  self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"menu";
    LeftMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[LeftMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = GreenColor;
    cell.label.text =self.dataSourceArr[indexPath.row];
    cell.leftImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"我的@icon_0%d",indexPath.row+1]];
    cell.rightImage.X = self.width-30;
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"点击了 tableView的第 %ld 个cell", (long)indexPath.row);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *actionName = [NSString stringWithFormat:@"%d",indexPath.row+1];
    if (_menuActionBlock) {
        _menuActionBlock(actionName);
    }

}

- (void)HeaderViewTapClick
{
    if (_menuActionBlock) {
        _menuActionBlock(@"0");
    }
}
@end

@implementation LeftMenuTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(12, 12, 20, 20)];
        [self.contentView addSubview:self.leftImage];
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(self.leftImage.XWidth+10, 12, 200, 20)];
        self.label.font = fontSizeNormal;
        self.label.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.label];
        
        self.rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 12, 8, 13)];
        self.rightImage.image = [UIImage imageNamed:@"我的@icon_10"];
        [self.contentView addSubview:self.rightImage];
    }
    return self;
}

@end
